interface TreeHelperConfig {
  /** ID字段名 */ id: string;
  /** 子节点字段名 */ children: string;
  /** 父ID字段名 */ pid: string;
}

/** 默认配置 */
const DEFAULT_CONFIG: TreeHelperConfig = {
  id: "id",
  children: "children",
  pid: "pid",
};

/** 获取配置, Object.assign 从一个或多个源对象复制到目标对象 */
const getConfig = (config: Partial<TreeHelperConfig>) => Object.assign({}, DEFAULT_CONFIG, config);

/**
 * 列表数组(扁平化数据)转换成Tree结构数据
 * @param list 数组
 * @param config 配置
 * @returns Tree结构数据
 */
export function listToTree<T = any>(list: any[], config: Partial<TreeHelperConfig> = {}): T[] {
  const conf = getConfig(config) as TreeHelperConfig;
  const nodeMap = new Map();
  const result: T[] = [];
  const { id, children, pid } = conf;

  for (const node of list) {
    node[children] = node[children] || [];
    nodeMap.set(node[id], node);
  }
  for (const node of list) {
    const parent = nodeMap.get(node[pid]);
    (parent ? parent[children] : result).push(node);
  }
  return result;
}

/**
 * Tree结构数据扁平化
 * @param tree Tree结构数据
 * @param config 配置
 * @returns 列表扁平化数据
 */
export function treeToList<T = any>(tree: any, config: Partial<TreeHelperConfig> = {}): T {
  config = getConfig(config);
  const { children } = config;
  const result: any = [...tree];
  for (let i = 0; i < result.length; i++) {
    if (!result[i][children!]) continue;
    result.splice(i + 1, 0, ...result[i][children!]);
  }
  return result;
}

/**
 * 查询树结构中的满足条件的数据节点
 * @param tree 需要过虑的Tree数据
 * @param func 过虑逻辑
 * @param config 配置
 * @returns Tree数据节点
 */
export function findNode<T = any>(tree: any, func: (node: any) => boolean, config: Partial<TreeHelperConfig> = {}): T | null {
  config = getConfig(config);
  const { children } = config;
  const list = [...tree];
  for (const node of list) {
    if (func(node)) return node;
    node[children!] && list.push(...node[children!]);
  }
  return null;
}

/**
 * 查询Tree结构数据中所有满足条件的节点
 * @param tree Tree结构数据
 * @param func 查询逻辑函数
 * @param config 配置
 * @returns 匹配到的所有结果节点的数组
 */
export function findNodeAll<T = any>(tree: any, func: (node: any) => boolean, config: Partial<TreeHelperConfig> = {}): T[] {
  config = getConfig(config);
  const { children } = config;
  const list = [...tree];
  const result: T[] = [];
  for (const node of list) {
    func(node) && result.push(node);
    node[children!] && list.push(...node[children!]);
  }
  return result;
}

/**
 * 过虑树结构数据
 * @param tree 需要过虑的Tree数据
 * @param func 过虑逻辑
 * @param config 配置
 * @returns Tree数据
 */
export function filter<T = any>(tree: T[], func: (n: T) => boolean, config: Partial<TreeHelperConfig> = {}): T[] {
  // 获取配置
  config = getConfig(config);
  const children = config.children as string;

  function listFilter(list: T[]) {
    return list
      .map((node: any) => ({ ...node }))
      .filter((node) => {
        // 递归调用 对含有children项  进行再次调用自身函数 listFilter
        node[children] = node[children] && listFilter(node[children]);
        // 执行传入的回调 func 进行过滤
        return func(node) || (node[children] && node[children].length);
      });
  }

  return listFilter(tree);
}

/**
 * 递归遍历树结构
 * @param treeDatas 树
 * @param callBack 回调
 * @param parentNode 父节点
 */
export function eachTree(treeDatas: any[], callBack: (node: any, parentNode: any) => any | undefined, parentNode = {}) {
  treeDatas.forEach((element) => {
    const newNode = callBack(element, parentNode) || element;
    if (element.children) {
      eachTree(element.children, callBack, newNode);
    }
  });
}
